<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class menu extends My_Controller {

    var $name = 'menu';
    
    public function __construct()
    {
        parent::__construct();
        //$this->load->model('menu_model');
    }
    
	public function index()
	{
        $this->list_menu(0);
	}
    
    function display_orther($curent_menu,$menu_up){
        $this->load->model('menu_model');
        
        $menu1 = $this->menu_model->getMenuById($curent_menu);
        $menu2 = $this->menu_model->getMenuById($menu_up);
        $data1 = array('display_order'=> $menu2->display_order);
        $this->menu_model->updateMenu($curent_menu,$data1);
        $data2 = array('display_order'=> $menu1->display_order);
        $this->menu_model->updateMenu($menu_up,$data2);
        
        redirect('menu');
    }
    
    function status($status = '',$id=0){
        $this->load->model('menu_model');
        switch($status){
            case 'approve':
                $data = array('status'=> 1);
                $this->menu_model->updateMenu($id,$data);
            break;
            
            case 'unapprove':
                $data = array('status'=> 0);
                $this->menu_model->updateMenu($id,$data);
            break;
            
            case 'delete':
                $data = array('status'=> -1);
                $this->menu_model->updateMenu($id,$data);
            break;
            
            default:
            
            break;
        }
        redirect('menu');
    }
    
    function list_menu($menuId = 0)
    {
        //$menu = $this->menu_model->menuParent($menuId,false);
           //var_dump($menu);
        $this->load->library('JimMenu');
        $jmenu = new JimMenu();
        
        $menu = $jmenu->menuParent($menuId,false);
        $menu_html = $jmenu->html_menu_admin('default/menu/theme_list_menu_table', $menu);
        //echo $menu_html;
       $data['menu'] = $menu_html;
       
       $data_sidebar = array(
            'menu' => 'menu'
       );
       $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',$data_sidebar);
       $this->template->parse_view('content', 'default/menu/list-menu-content', $data);
       // Render template
        $this->template->render();
    }
    
    function insert(){
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model('menu_model');
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('menu_name', 'Role Name', 'required|max_length[100]');
            $this->form_validation->set_rules('menu_link', 'Link', 'required|max_length[255]');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');
                echo 'khong thanh cong';
            }else{
                $parent = $this->input->post('parent');
                $title = $this->input->post('menu_name');
                $link = $this->input->post('menu_link');
                //------------
                $parent_ins = 0;
                $dirparent_ins = 0;
                
                if($parent == 0)
                {
                    $parent_ins = 0;
                    $dirparent_ins = 0;
                }
                else
                {
                    $menu_chose = $this->menu_model->getMenuById($parent);
                    if($menu_chose->parent == 0 && $menu_chose->dirparent == 0)
                    {
                        $parent_ins = $menu_chose->id;
                        $dirparent_ins = $menu_chose->id;
                    }
                    else
                    {
                        $parent_ins = $menu_chose->id;
                        $dirparent_ins = $menu_chose->dirparent;
                    }
                }

                $insertdata = array(
                    'parent' => $parent_ins,
                    'dirparent' => $dirparent_ins,
                    'title' => $title,
                    'parameters' => '',
                    'link' => $link,
                    'display_order' => 0,
                    'status' => 0
                );
                $new_menuId = $this->menu_model->insertMenu($insertdata);
                $updatedata = array('display_order'=> $new_menuId);
                $this->menu_model->updateMenu($new_menuId,$updatedata);
                redirect('menu/list_menu', 'location', 301);
            }
        }
        
        $data_sidebar = array(
            'menu' => 'menu'
        );
        $data = array(
            'action'=> site_url('menu/insert'),
            'html_option_selectbox'=>$this->menu_model->htmlShowOptionMenu(0,'- ','',0)
        );
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',$data_sidebar);
        $this->template->parse_view('content', 'default/menu/insert_menu', $data);
       // Render template
        $this->template->render();
    }
    
    function edit($menuId){
        $this->load->model('menu_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $menu = $this->menu_model->getMenuById($menuId,false);
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('menu_name', 'Role Name', 'required|max_length[100]');
            $this->form_validation->set_rules('menu_link', 'Link', 'required|max_length[255]');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');
                echo 'khong thanh cong';
            }else{
                $parent = $this->input->post('parent');
                $title = $this->input->post('menu_name');
                $link = $this->input->post('menu_link');
                
                $parent_ins = 0;
                $dirparent_ins = 0;
                
                if($parent == 0){
                    $parent_ins = 0;
                    $dirparent_ins = 0;
                    //$this->site_model->update_menu_parent($menu->id,$menu->id);
                    $parent_item = $menu['id'];
                    $dirparent_item = $menu['id'];
                }else{
                    $menu_chose = $this->menu_model->getMenuById($parent);
                    if($menu_chose->parent == 0 && $menu_chose->dirparent == 0){
                        $parent_ins = $menu_chose->id;
                        $dirparent_ins = $menu_chose->id;
                        // update lai toan bo menu con
                        //$this->site_model->update_menu_parent($menu->id,$dirparent_ins);
                        $parent_item = $menu['id'];
                        $dirparent_item = $dirparent_ins;
                    }else{
                        $parent_ins = $menu_chose->id;
                        $dirparent_ins = $menu_chose->dirparent;
                        // update lai toan bo menu con
                        //$this->site_model->update_menu_parent($menu->id,$dirparent_ins);
                        $parent_item = $menu['id'];
                        $dirparent_item = $dirparent_ins;
                    }
                }
                
                $updatedata = array(
                    'parent' => $parent_ins,
                    'dirparent' => $dirparent_ins,
                    'title' => $title,
                    'parameters' => '',
                    'link' => $link
                );
                
                $this->menu_model->updateMenu($menu['id'],$updatedata);
                $this->menu_model->updateMenuParent($parent_item,$dirparent_item);

                redirect('menu/list_menu', 'location', 301);
            }
        }
        
        $data_sidebar = array(
            'menu' => 'menu'
        );
        
        $data = array(
            'action'=> site_url('menu/edit/'.$menuId),
            'menu_data' => $menu,
            'html_option_selectbox' => $this->menu_model->htmlShowOptionMenu(0,'- ','',$menuId,$menu['parent'])
        );
        
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',$data_sidebar);
        $this->template->parse_view('content', 'default/menu/insert_menu', $data);
       // Render template
        $this->template->render();
    }
    
    function chooseMenu(){
        $this->load->model('menu_model');
        
        $menu = $this->menu_model->loadMenuController();

        $html_menu = $this->load->view('default/menu/theme_menu_controller',array('menu'=>$menu),true);
        $data = array();
        $data['html_menu'] = $html_menu;

        $this->load->view('default/menu/popup_choose_menu_panel',$data);
    }
    
    function chooseMenuCustomLink(){
        
        $this->load->view('default/menu/popup_choose_customlink',array());
    }
    //================================================================
  
    
    function deletemenu()
    {
        $this->load->model('menu_model');
        $menu_id = $this->input->get('id');
        $act = $this->input->get('act');
        
        if($act == 'do')
        {
            $this->menu_model->deleteMenu($menu_id);
            $this->menu_model->update_menu_parent_delete($menu_id);
            redirect('option=menu&view=menumanager', 'location', 301);
        }
        else
        {
            redirect('option=menu&view=menumanager', 'location', 301);
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */